/**
 * Project: ameba.web
 * 
 * File Created at 2015年12月30日
 * $Id$
 * 
 * Copyright 2015 dface.cn Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * dface Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with www.dface.cn
 */
package cn.dface.ameba.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.dface.ameba.biz.service.ApplicationService;
import cn.dface.ameba.biz.vo.AppDeployRecord;
import cn.dface.ameba.biz.vo.ApplicationApiVo;
import cn.dface.ameba.biz.vo.ApplicationTagVo;
import cn.dface.ameba.biz.vo.ApplicationVersionVo;
import cn.dface.ameba.dal.entity.EnumApiUsage;
import cn.dface.ameba.dal.entity.EnumAppUsage;
import cn.dface.ameba.web.vo.DeployResult;
import cn.dface.ameba.web.vo.ResponseResult;
import cn.dface.framework.core.enums.ResultEnums;
import cn.dface.framework.core.page.PageModel;
import cn.dface.framework.core.vo.ResultVo;

/**
 * ApplicationController
 * 
 * @author LVZY
 */
@RestController
@RequestMapping("/app")
public class ApplicationController {

    @Autowired
    ApplicationService appService;
    
    @RequestMapping(value = "/tag/list")
    public List<ApplicationTagVo> getApplicationTagList() {
        return this.appService.getApplicationTag();
    }

    @RequestMapping(value = "/tag/usage/{usage}/list")
    public List<ApplicationTagVo> getApplicationTagListByUsage(@PathVariable("usage") int usage) {
        return appService.getApplicationTagByUsage(EnumAppUsage.valueOf(usage));
    }

    @RequestMapping(value = "/tag/action/add", method = RequestMethod.POST)
    public ResponseResult addApplicationTag(@RequestParam(value = "name") String name,
                                            @RequestParam(value = "description") String description,
                                            @RequestParam(value = "usage") int usage) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ApplicationTagVo appTag = new ApplicationTagVo();
        appTag.setName(name);
        appTag.setDescription(description);
        appTag.setUsage(usage);
        appTag = this.appService.saveApplicationTag(appTag);
        if (null != appTag) {
            result.setObj(appTag);
        } else {
            result.setSucceeded(false);
        }
        return result;
    }
    
    @RequestMapping(value = "/tag/id/{id}", method = RequestMethod.POST)
    public ResponseResult updateApplicationTag(@PathVariable("id") Integer id,
                                               @RequestParam(value = "name") String name,
                                               @RequestParam(value = "description") String description) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ApplicationTagVo appTag = new ApplicationTagVo();
        appTag.setSid(id);
        appTag.setName(name);
        appTag.setDescription(description);
        appTag = this.appService.saveApplicationTag(appTag);
        if (null != appTag) {
            result.setObj(appTag);
        } else {
            result.setSucceeded(false);
        }
        return result;
    }

    @RequestMapping(value = "/tag/id/{id}", method = RequestMethod.DELETE)
    public ResponseResult removeApplicationTag(@PathVariable("id") Integer id) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ResultVo resultVo = appService.removeApplicationTagById(id);
        if (resultVo.getStatus() != ResultEnums.SUCESS.ordinal()) {
            result.setSucceeded(false);
            result.setMessage(resultVo.getErrorMsg());
        }
        return result;
    }
    
    @RequestMapping(value = "/tag/usage/{usage}/version/list")
    public List<ApplicationTagVo> getApplicationTagWithVersionList(@PathVariable("usage") int usage) {
        return this.appService.getApplicationTagWithVersionByUsage(EnumAppUsage.valueOf(usage));
    }
    
    @RequestMapping(value = "/tag/id/{tagId}/version/list")
    public List<ApplicationVersionVo> getApplicationVersionList(@PathVariable("tagId") Integer tagId) {
        return this.appService.getApplicationVersionByAppTagId(tagId);
    }
    
    @RequestMapping(value = "/tag/id/{tagId}/version/action/add", method = RequestMethod.POST)
    public ResponseResult addApplicationVersion(@PathVariable(value = "tagId") Integer tagId,
                                                @RequestParam(value = "version") String version,
                                                @RequestParam(value = "description") String description,
                                                @RequestParam(value = "deployUrl") String deployUrl,
                                                @RequestParam(value = "status") int status) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ApplicationVersionVo appVersion = new ApplicationVersionVo();
        ApplicationTagVo appTag = new ApplicationTagVo();
        appTag.setSid(tagId);
        appVersion.setAppTag(appTag);
        appVersion.setVersion(version);
        appVersion.setDescription(description);
        appVersion.setDeployUrl(deployUrl);
        appVersion.setStatus(status);
        appVersion = this.appService.saveApplicationVersion(appVersion);
        if (null != appVersion) {
            result.setObj(appVersion);
        } else {
            result.setSucceeded(false);
        }
        return result;
    }
    
    @RequestMapping(value = "/version/id/{id}", method = RequestMethod.POST) 
    public ResponseResult updateApplicationVersion(@PathVariable("id") Integer id,
                                                   @RequestParam(value = "version") String version,
                                                   @RequestParam(value = "description") String description,
                                                   @RequestParam(value = "deployUrl") String deployUrl) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ApplicationVersionVo appVersion = new ApplicationVersionVo();
        appVersion.setSid(id);
        appVersion.setVersion(version);
        appVersion.setDescription(description);
        appVersion.setDeployUrl(deployUrl);
        appVersion = this.appService.saveApplicationVersion(appVersion);
        if (null != appVersion) {
            result.setObj(appVersion);
        } else {
            result.setSucceeded(false);
        }
        return result;
    }
    
    @RequestMapping(value = "/version/id/{id}", method = RequestMethod.DELETE) 
    public ResponseResult removeApplicationVersion(@PathVariable("id") Integer id) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ResultVo resultVo = appService.removeApplicationVersionById(id);
        if (resultVo.getStatus() != ResultEnums.SUCESS.ordinal()) {
            result.setSucceeded(false);
            result.setMessage(resultVo.getErrorMsg());
        }
        return result;
    }
    
    @RequestMapping(value = "/version/id/{id}/action/deploy")
    public ResponseResult deployApplicationVersion(@PathVariable(value = "id") Integer id) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ResultVo resultVo = this.appService.deployByAppVersionId(id);
        if (resultVo.getStatus() != ResultEnums.SUCESS.ordinal()) {
            result.setSucceeded(false);
            result.setMessage(resultVo.getErrorMsg());
        }
        return result;
    }
    
    @RequestMapping(value = "/version/id/{id}/action/log")
    public DeployResult getApplicationDeployLog(@PathVariable(value = "id") Integer id) {
        DeployResult result = new DeployResult();
        result.setSucceeded(true);
        ResultVo resultVo = this.appService.getDeployLogByAppVersionId(id);
        if (resultVo.getStatus() != ResultEnums.SUCESS.ordinal()) {
            result.setSucceeded(false);
            result.setMessage(resultVo.getErrorMsg());
        } else {
            result.setDeployRecord((AppDeployRecord) resultVo.getObject());
        }
        return result;
    }
    
    @RequestMapping(value = "/version/id/{versionId}/api/usage/{usage}/page")
    public PageModel getApplicationApiPage(@PathVariable(value = "versionId") Integer versionId,
                                           @PathVariable(value = "usage") int usage,
                                           @RequestParam(value = "fuzzyTitle", required = false) String fuzzyTitle,
                                           @RequestParam(value = "pageNo") Integer pageNo,
                                           @RequestParam(value = "pageSize") Integer pageSize) {
        return this.appService.getPageOfApplicationApiByAppVersionIdAndUsage(versionId,
                EnumApiUsage.valueOf(usage), fuzzyTitle, pageNo, pageSize);
    }
    
    @RequestMapping(value = "/version/id/{versionId}/api/usage/{usage}/page/count")
    public Integer getApplicationApiPageCount(@PathVariable(value = "versionId") Integer versionId,
                                              @PathVariable(value = "usage") int usage,
                                              @RequestParam(value = "fuzzyTitle") String fuzzyTitle,
                                              @RequestParam(value = "pageSize") Integer pageSize) {
        return this.appService.getPageCountOfApplicationApiByAppVersionIdAndUsage(versionId,
                EnumApiUsage.valueOf(usage), fuzzyTitle, pageSize);
    }
                
    
    @RequestMapping(value = "/version/id/{versionId}/api/action/add", method = RequestMethod.POST)
    public ResponseResult addApplicationApi(@PathVariable(value = "versionId") Integer versionId,
                                            @RequestParam(value = "title") String title,
                                            @RequestParam(value = "content") String content,
                                            @RequestParam(value = "usage") int usage) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ApplicationApiVo appApi = new ApplicationApiVo();
        ApplicationVersionVo AppVersion = new ApplicationVersionVo();
        AppVersion.setSid(versionId);
        appApi.setAppVersion(AppVersion);
        appApi.setTitle(title);
        appApi.setContent(content);
        appApi.setUsage(usage);
        appApi = this.appService.saveApplicationApi(appApi);
        if (null != appApi) {
            result.setObj(appApi);
        } else {
            result.setSucceeded(false);
        }
        return result;
    }
    
    @RequestMapping(value = "/api/id/{id}", method = RequestMethod.POST)
    public ResponseResult updateApplicationApi(@PathVariable(value = "id") Integer id,
                                               @RequestParam(value = "title") String title,
                                               @RequestParam(value = "content") String content) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ApplicationApiVo appApi = new ApplicationApiVo();
        appApi.setSid(id);
        appApi.setTitle(title);
        appApi.setContent(content);
        appApi = this.appService.saveApplicationApi(appApi);
        if (null != appApi) {
            result.setObj(appApi);
        } else {
            result.setSucceeded(false);
        }
        return result;
    }
    
    @RequestMapping(value = "/api/id/{id}", method = RequestMethod.DELETE)
    public ResponseResult removeApplicationApi(@PathVariable(value = "id") Integer id) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        ResultVo resultVo = this.appService.removeApplicationApiById(id);
        if (resultVo.getStatus() != ResultEnums.SUCESS.ordinal()) {
            result.setSucceeded(false);
            result.setMessage(resultVo.getErrorMsg());
        }
        return result;
    }

}
